﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using StyleMVVM.UnitTests.TestClasses.ValidationObjects;
using StyleMVVM.DependencyInjection;
using StyleMVVM.Validation;
#if NETFX_CORE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;

#else
using Microsoft.VisualStudio.TestTools.UnitTesting;

#endif

namespace StyleMVVM.UnitTests.Validation
{
	[TestClass]
	public class ValidationServiceTests : BaseTestClass
	{
		[TestMethod]
		public void SimpleValidationObjectTest()
		{
			IValidationService validationService = Container.Locate<IValidationService>();

			SimpleValidationObject simpleValidationObject = new SimpleValidationObject();

			IValidationContext context = validationService.GetValidationContext(simpleValidationObject);

			Assert.AreEqual(ValidationState.Invalid, context.State);
			Assert.AreEqual(3, context.RuleResults().Count());

			simpleValidationObject.TestInt = 4;
			simpleValidationObject.Name = "Blah";

			Assert.AreEqual(ValidationState.Valid, context.State);
		}

		[TestMethod]
		public void MethodSimpleTest()
		{
			IValidationService validationService = Container.Locate<IValidationService>();

			ManualResetEvent waitEvent = new ManualResetEvent(false);
			MethodSimpleTestObject testObject = new MethodSimpleTestObject();

			IValidationContext context = validationService.GetValidationContext(testObject);

			Assert.IsTrue(context.Validating);
			Assert.AreEqual(ValidationState.Invalid, context.State);

			waitEvent.WaitOne(150);

			Assert.IsFalse(context.Validating);

			testObject.Name = "Blah";
			testObject.TestInt = 4;

			Assert.AreEqual(ValidationState.InvalidRequired, context.State);
			Assert.IsTrue(context.Validating);

			waitEvent.WaitOne(150);

			Assert.IsFalse(context.Validating);
			Assert.AreEqual(ValidationState.Valid, context.State);
		}
	}
}